#include <QApplication>
#include <QPushButton>
#include <QDesktopWidget>
#include <QFontDatabase>
#include "publicFunc.h"
#include <QDesktopWidget>
#include <QFontDatabase>
#include <QTextCodec>
#include <QFontDatabase>
#include "CAppUiConfig.h"
#include "QUnits.h"

const char *buf[20] = {
		"/usr/share/fonts/dejavu/DejaVuSans.ttf",			// KO
		"/usr/share/fonts/dejavu/DejaVuSans-Bold.ttf",		// NO
		"/usr/share/fonts/dejavu/DejaVuSans-BoldOblique.ttf", // NO
		"/usr/share/fonts/dejavu/DejaVuSans-ExtraLight.ttf",// NO
		"/usr/share/fonts/dejavu/DejaVuSans-Oblique.ttf",// NO

		"/usr/share/fonts/dejavu/DejaVuSansMono-Oblique.ttf",// NO
		"/usr/share/fonts/dejavu/DejaVuSansMono.ttf",// NO
		"/usr/share/fonts/dejavu/DejaVuSansMono-Bold.ttf",// NO
		"/usr/share/fonts/dejavu/DejaVuSansMono-BoldOblique.ttf",// OK

		"/usr/share/fonts/dejavu/DejaVuSerif.ttf",// NO
		"/usr/share/fonts/dejavu/DejaVuSerif-Bold.ttf",// NO
		"/usr/share/fonts/dejavu/DejaVuSerif-BoldItalic.ttf",// NO
		"/usr/share/fonts/dejavu/DejaVuSerif-Italic.ttf",// NO

		"/usr/share/fonts/dejavu/DejaVuSansCondensed-Bold.ttf",// NO
		"/usr/share/fonts/dejavu/DejaVuSansCondensed-BoldOblique.ttf",// NO
		"/usr/share/fonts/dejavu/DejaVuSansCondensed-Oblique.ttf",// NO

		"/usr/share/fonts/dejavu/DejaVuSerifCondensed-Bold.ttf",// OK
		"/usr/share/fonts/dejavu/DejaVuSerifCondensed-BoldItalic.ttf",//NO
		"/usr/share/fonts/dejavu/DejaVuSerifCondensed-Italic.ttf",
		"/usr/share/fonts/dejavu/DejaVuSerifCondensed.ttf",
};


static bool isRun = true;


#include <QKeyEvent>

static void sendKey(int key)
{

	QWidget* obj = QApplication::focusWidget();
	if(nullptr != obj)  // 要注意判空,否则会崩溃
	{
		QKeyEvent keyevent(QEvent::KeyPress, key, Qt::NoModifier, "模拟");//数字 字符
		QApplication::sendEvent(obj, &keyevent);

		QKeyEvent keyeventR(QEvent::KeyRelease, key, Qt::NoModifier, "模拟");//数字 字符
		QApplication::sendEvent(obj, &keyeventR);

	}

}


static void* AnalogKeyInput(void *data)
{

	int key0_9[] = {Qt::Key_0, Qt::Key_1, Qt::Key_2,Qt::Key_3 ,Qt::Key_4 ,Qt::Key_5, Qt::Key_6,Qt::Key_7, Qt::Key_8, Qt::Key_9};
	while(isRun){

		string inKey;

		cin >> inKey;

		qDebug() << inKey.c_str();

		if(inKey == "q"){
			sendKey(Qt::Key_F6);
			qDebug() << "sendKey(Qt::Key_F6)";
		} else if(inKey == "w"){
			sendKey(Qt::Key_F5);
			qDebug() << "sendKey(Qt::Key_F5)";
		} else if(QString(inKey.c_str()).toInt() >= 0 && QString(inKey.c_str()).toInt() <= 9){
			sendKey(key0_9[QString(inKey.c_str()).toInt()]);
			qDebug() << "sendKey(key0_9[QString(inKey.c_str()).toInt()])";
		}


	}
	return NULL;

}

int main(int argc, char *argv[])
{

    QApplication a(argc, argv);
	// 安装消息处理程序
    qInstallMessageHandler(myMessageOutput);
	// 设置编码格式
	QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));



    return a.exec();
}
